package hotLeetcode;

import java.util.Stack;

public class H394Leetcode {
    public static String decodeString(String s) {

        Stack<Integer> counts  = new Stack<>();
        Stack<String> results = new Stack<>();
        StringBuilder current = new StringBuilder();
        int multi = 0;
        for (char c : s.toCharArray()) {
            if (Character.isDigit(c)) {
                multi = multi * 10 + (c - '0');
            } else if (c == '[') {
                counts.push(multi);
                results.push(current.toString());
                multi = 0;
                current = new StringBuilder();
            } else if (c == ']') {
                //从栈中弹出 multi 和 current
                Integer count = counts.pop();
                String lastRes = results.pop();
                current = new StringBuilder(lastRes + current.toString().repeat(count));
            } else {
                //如果是普通字符 直接拼接到 current
                current.append(c);
            }


        }

        return current.toString();

    }

    public static void main(String[] args) {
        String s = "3[a]12[bc]";
        H394Leetcode.decodeString(s);

    }

}
